package doughnut.xc.Demo;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 使用jasper模版导出pdf
 */
public class JasperDemo {
    public static void main(String[] args) throws Exception {
        // 模版路径
        File file = ResourceUtils.getFile("classpath:pdf_template/test.jasper");
        // Map 准备填充数据
        Map<String, Object> mapData = new HashMap<>();
        mapData.put("userNameP", "张三");
        mapData.put("phoneP","15564351598");
        JasperPrint jasperPrint = JasperFillManager.fillReport(file.getPath(), mapData, new JREmptyDataSource());
        FileOutputStream fos = new FileOutputStream("D:\\test.pdf");
        JasperExportManager.exportReportToPdfStream(jasperPrint, fos);
        fos.close();
    }

}
